//
// Created by 王筱童 on 2022/5/9.
//

#ifndef TINY_REDIS_SERVER_CMAKE_REDIS_CONNECTION_H
#define TINY_REDIS_SERVER_CMAKE_REDIS_CONNECTION_H
#include <asio.hpp>
#include <string>
class redis_server;
class redis_connection {
private:
    asio::ip::tcp::socket socket;
    redis_server *server;
    std::size_t index;
    std::size_t db_index;
    bool close_spawn= false;

    asio::awaitable<std::string> read_data();
    asio::awaitable<std::string> exec_command(const std::string&data);
    asio::awaitable<void> write_result(const std::string& result);
public:
    redis_connection(asio::ip::tcp::socket&& _sock,redis_server* _server,std::size_t _index);
    asio::awaitable<void> start();
    void close_coro();
    asio::ip::tcp::socket & get_socket();
    std::size_t& db_seq(){
        return db_index;
    }
};


#endif //TINY_REDIS_SERVER_CMAKE_REDIS_CONNECTION_H
